<template>
  <div class="document-entry" v-if="fileUrl">
    <div class="left">
      <word-tree :nodes="treeData" @open-node="openNode" />
    </div>
    <div class="center">
      <div class="file-view" ref="wordViewer">
        <WordViewer :nodes="treeData" />
      </div>
    </div>
    <div class="right">
      <div class="word-form" v-if="currentNode">
        <RevForm :fields="currentNode.form" :config="{ labelPosition: 'top' }" />
      </div>
      <el-button class="btn" type="primary" @click="doSave()">保存</el-button>
    </div>
  </div>
</template>

<script>
import WordTree from './components/WordTree.vue'
import WordViewer from './components/WordViewer.vue'
import { getWordTree, saveWordTree } from '@/api/file'

export default {
  name: 'RevRendererWord',
  components: { WordTree, WordViewer },
  props: {
    url: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      tempurl: '',
      treeData: [],
      info: {},
      currentNode: null
    }
  },
  computed: {
    fileUrl() {
      return this.tempurl || this.url
    },
    preUrl() {
      return this.fileUrl.replace('word:', '')
    }
  },
  watch: {
    fileUrl() {
      this.getTree()
    }
  },
  created() {
    this.getTree()
  },
  methods: {
    // 查询树数据
    async getTree() {
      if (!this.fileUrl) {
        return
      }
      const res = await getWordTree({
        path: this.fileUrl
      })
      this.info = res.info
      this.treeData = res.info.nodes
    },
    loadData(url) {
      this.tempurl = url
    },
    openNode(node) {
      this.currentNode = node
      this.scrollTo(node)
    },
    scrollTo(node) {
      const viewer = this.$refs.wordViewer
      if (viewer) {
        const cdom = this.$refs.wordViewer.querySelector(`[nodeid="${node.id}"]`)
        if (cdom) {
          const scrollTop =
            cdom.getBoundingClientRect().top + viewer.scrollTop - viewer.getBoundingClientRect().y
          viewer.scrollTop = scrollTop - 3
        }
      }
    },
    async doSave() {
      await saveWordTree({
        path: this.fileUrl,
        info: this.info
      })
      this.$message({
        type: 'success',
        message: '保存成功'
      })
    }
  }
}
</script>

<style scoped lang="less">
.document-entry {
  height: 100%;

  display: flex;

  .left {
    flex: 0 0 250px;
  }
  .center {
    position: relative;
    flex: 1;
    height: 100%;
    border-right: 1px solid #e8eaec;

    .img-content {
      display: inline-flex;
      min-height: 50px;
      min-width: 50px;
      border: 1px solid #e8eaec;

      justify-content: center;
      align-items: center;
    }

    .file-view {
      width: 100%;
      height: 100%;
      overflow: auto;
      padding: 10px;
    }

    .graph-view {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      background-color: #fff;
      padding: 20px;
    }

    .close {
      position: absolute;
      right: 15px;
      top: 10px;
      font-size: 20px;
      cursor: pointer;
      z-index: 21;
      background: #fff;
      color: #000;
      display: inline-block;
      line-height: 20px;
      text-align: center;
      padding: 0 3px;
      border-radius: 8px;
    }
  }
  .right {
    position: relative;
    flex: 0 0 300px;
    padding: 5px;

    .btn {
      position: absolute;
      left: 10px;
      bottom: 30px;
    }
  }
}
</style>
